- name: create master
  become: true
  become_user: "{{ buildmaster_user }}"
  command: creates={{ buildmaster_dir }} buildbot create-master -r {{ buildmaster_dir }}

- name: upload master config
  become: true
  become_user: "{{ buildmaster_user }}"
  template: src={{ buildmaster_template }} dest={{ buildmaster_dir }}/master.cfg owner={{ buildmaster_user }} group={{ buildmaster_user }}

- name: check master config
  become: true
  become_user: "{{ buildmaster_user }}"
  command: buildbot checkconfig {{ buildmaster_dir }}

#- name: upgrade master
#  command: buildbot upgrade-master {{ buildmaster_dir }}

- name: generate buildmaster service file
  template: src=buildmaster.service.j2 dest=/lib/systemd/system/buildmaster.service owner=root group=root mode=0644
  register: buildmaster_service

- name: reload systemd
  command: systemctl daemon-reload
  when: buildmaster_service.changed

- name: start and enable buildmaster service
  service: name=buildmaster enabled=yes state={{ (buildmaster_service.changed) | ternary('restarted','started') }}
  # Workaround for https://pagure.io/taskotron/issue/139
  # Otherwise, Ansible playbook will fail if it has to wait for another buildmaster.service start try
  register: result
  until: result|succeeded
  delay: 70
  retries: 3

- name: reconfig master
  become: true
  become_user: "{{ buildmaster_user }}"
  command: buildbot reconfig {{ buildmaster_dir }}
